from django.shortcuts import render
from django.views import View
from django import http
from booktest.models import BookInfo

# Create your views here.


class TestTemplateView(View):

    def get(self, request):
        return render(request, 'test1.html')


class Test2TemplateView(View):

    def get(self, request):
        context = {
            "subject1" : "Python入门到放弃",
            "subject2" : "Java入门到放弃",
            "subject3" : "前端入门到放弃",
            "subject4" : "Vue.js入门到放弃",
            "subject5" : "PHP入门到放弃"
        }
        return render(request, 'test2.html', context)


class Test3TemplateView(View):

    def get(self, request):
        books = BookInfo.objects.filter(is_delete=False).all()
        context = {
            "books" : books
        }
        print(context)
        response = render(request, 'test3.html', context)
        response.set_cookie('username', value='handsome_yang', max_age=3600)
        request.session['username'] = 'handsome_yang'
        return response


class CookiesTestView(View):

    def get(self, request):
        cookie = request.COOKIES.get("username")
        print(cookie)
        return http.HttpResponse("测试cookies")


class SessionTestView(View):

    def get(self, request):
        username = request.session.get('username')
        print(username)
        return http.HttpResponse("测试session")

